package org.example.面向对象.dao;

import org.apache.log4j.Logger;
import org.example.面向对象.bean.EmpInfo;
import org.example.面向对象.db.DBManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmpInfoDAO {
    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;
    //产生一个日志对象
    private Logger logger = Logger.getLogger(EmpInfoDAO.class);

    public void saveEmp(EmpInfo ei){
        String  sql = "insert into emp values(null,?,?,?,?,?,?,?,1)";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,ei.getEname());
            ps.setString(2,ei.getJob());
            ps.setInt(3,ei.getMgr());
            ps.setString(4,ei.getHireDate());
            ps.setDouble(5,ei.getSal());
            ps.setDouble(6,ei.getComm());
            ps.setInt(7,ei.getDeptno());
            ps.executeUpdate();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public void updateEmp(EmpInfo ei){
        String sql = "update emp set ename=?,sal=? where empno=?";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,ei.getEname());
            ps.setDouble(2,ei.getSal());
            ps.setInt(3,ei.getEmpNo());
            ps.executeUpdate();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public void deleteEmp(int empNo){
        String sql = "update emp set empstate=0 where empno=?";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,empNo);
            ps.executeUpdate();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

    public EmpInfo findEmpById(int empNo){
        EmpInfo ei = new EmpInfo();
        String sql = "select *from emp where empno=?";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,empNo);
            rs = ps.executeQuery();
            if (rs.next()){
                ei.setEmpNo(rs.getInt(1));
                ei.setEname(rs.getString(2));
                ei.setJob(rs.getString(3));
                ei.setMgr(rs.getInt(4));
                ei.setHireDate(rs.getString(5));
                ei.setSal(rs.getDouble(6));
                ei.setComm(rs.getDouble(7));
                ei.setDeptno(rs.getInt(8));
                ei.setEmpState(1);
            }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
        return ei;
    }

    public List<EmpInfo> findAllEmp(){
        logger.debug("开始记录日志");
        logger.info("用户开始查询");
        logger.warn("有异常信息了");
        logger.error("有错误信息了");
        List<EmpInfo> list = new ArrayList<>();
        String sql = "select * from emp where empstate=1";
        conn = DBManager.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                EmpInfo ei = new EmpInfo();
                ei.setDeptno(rs.getInt(1));
                ei.setEname(rs.getString(2));
                ei.setJob(rs.getString(3));
                ei.setMgr(rs.getInt(4));
                ei.setHireDate(rs.getString(5));
                ei.setSal(rs.getDouble(6));
                ei.setComm(rs.getDouble(7));
                ei.setDeptno(rs.getInt(8));
                ei.setEmpState(1);
                list.add(ei);
                logger.debug("查询出来一个"+ei);
            }
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
        return list;
    }
}
